package com.seedwill.common.dozer;

import org.dozer.CustomConverter;

/**
 * DOZER转换器
 * 
 * 数值DOUBLE转换
 * 
 * @author kdyi
 *
 */
public class DoubleConverter extends ABaseConverterSupport implements CustomConverter {

	public DoubleConverter() {
	}

	//
	public Object convert(Object existingDestinationFieldValue,
			Object sourceFieldValue, Class destinationClass, Class sourceClass) {
		Object result = null;
		if(sourceFieldValue!=null) {
			try {
				if(sourceFieldValue instanceof org.apache.xmlbeans.XmlDouble) {
					result = ((org.apache.xmlbeans.XmlDouble) sourceFieldValue).getDoubleValue();
				}else {
					org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory.newInstance();
					if(sourceFieldValue instanceof Double) {
						obj.setDoubleValue((Double)(sourceFieldValue));
					}else {
						obj.setDoubleValue(Double.parseDouble(sourceFieldValue.toString()));
					}
					result = obj;
				}
			} catch (RuntimeException e) {
				log.warn(
						"Data convert exception[existingDestinationFieldValue:"
								+ existingDestinationFieldValue+(existingDestinationFieldValue !=null ? "("+existingDestinationFieldValue.getClass()+")" : "") + "]"
								+ "[sourceFieldValue:" + sourceFieldValue +(sourceFieldValue !=null ? "("+sourceFieldValue.getClass()+")" : "")+ "]"
								+ "[destinationClass:" + destinationClass + "]"
								+ "[sourceClass:" + sourceClass + "]", e);
				throw e;
			}
		}
		return result;
	}


}
